package org.linlinjava.litemall.db;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;

import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.kit.StringKit;
import org.beetl.sql.core.query.LambdaQuery.Property;

public class BeetlSqlUtils {
	 public static <T> String getFunctionName( SQLManager  sqlManager , Class<T> clazz , Property<T, ?> property) {
	        try {
	            Method declaredMethod = property.getClass().getDeclaredMethod("writeReplace");
	            declaredMethod.setAccessible(Boolean.TRUE);
	            SerializedLambda serializedLambda = (SerializedLambda) declaredMethod.invoke(property);
	            String method = serializedLambda.getImplMethodName();
	            String attr = null;
	            if (method.startsWith("get")) {
	                attr = method.substring(3);
	            } else {
	                attr = method.substring(2);
	            }
	            return sqlManager.getNc().getColName(clazz, StringKit.toLowerCaseFirstOne(attr));
	        } catch (ReflectiveOperationException e) {
	            throw new RuntimeException(e);
	        }

	    }
}
